package com.seio.edc.controller.statistics;

import com.alibaba.fastjson.JSON;
import com.nova.edc.model.energy.Gather;
import com.nova.edc.model.system.*;
import com.nova.edc.service.energy.*;
import com.nova.edc.service.system.*;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.nova.frame.utils.JdbcUtils;
import com.nova.frame.utils.StringUtils;
import com.seio.edc.common.Constants;
import com.seio.edc.util.DateTimeUtil;
import com.seio.edc.util.DateUtil;
import com.seio.edc.util.IntegerUtils;
import com.seio.edc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/3/24 0024.
 */
@Controller
@RequestMapping(value = "/charts")
public class ChartsController {

    @Autowired
    private CustomerMapService novaCustomerMapService;
    @Autowired
    private CustomerInfoService novaCustomerInfoService;
    @Autowired
    private MapAreaService mapAreaService;
    @Autowired
    private AreaPointService novaAreaPointService;
    @Autowired
    private GatherSummaryService novaGatherSummaryService;
    @Autowired
    private MenuService novaMenuService;
    @Autowired
    private GatherService novaGatherService;

    private static final int PAGE_SIZE = 5;

    @RequestMapping(value = "/chartslist.shtml")
    public ModelAndView countOne(HttpServletRequest request,
                                 HttpSession session, ModelMap modelMap) {
        Login login = (Login) session.getAttribute("sessionLogin");
        // 地图构建工具----获取所有省份信息
        MapArea mapArea = new MapArea();
        mapArea.setParentid(0);
        List<MapArea> map_provinces = mapAreaService.findMapAreaByExample(mapArea);
        modelMap.put("map_provinces", JSON.toJSONString(map_provinces));
        List<Map<String, Object>> positionList = null;
        if (login.getRoletype() != null && login.getRoletype() == ModelConstant.ROLE_GROUP) {
            positionList = novaCustomerMapService.findFirstCustomerMapGroup(login.getGroupcode());
        } else {
            positionList = novaCustomerMapService
                    .findFirstCustomerMap(login.getUsername());
        }
        Menu menu = novaMenuService.findByMenuname(ModelConstant.MENU_DATA);
        if (menu != null) {
            List<Menu> menuList = novaMenuService.findByParent(menu.getId());
            modelMap.put("menuList", menuList);
        }
        modelMap.put("positionList", JSON.toJSONString(positionList));
        modelMap.put("areaList", positionList);
        return new ModelAndView("statistics/chartslist", modelMap);
    }


    @RequestMapping(value = "/getEnergy.shtml")
    @ResponseBody
    public String getEnergy(HttpServletRequest request,
                            HttpSession session, ModelMap modelMap) {
        Login login = (Login) session.getAttribute("sessionLogin");
        Page page = new Page();
        page.setCurrentPage(1);
        page.setPageSize(1000);
        PageList<CustomerInfo> customerInfoPageList = novaCustomerInfoService
                .findCustomerInfoPageListByRole(login, 0,
                        "", null, page, "", "asc");
        List<CustomerInfo> customerInfoList = customerInfoPageList.getDatas();
        List<AreaPoint> areaPointList = novaAreaPointService.getByCompany(customerInfoList);
        String starttime = DateUtils.getDate2SStr(new Date()) + " 00:00:00";
        String endtime = DateUtils.getDate2SStr(new Date()) + " 23:59:59";
        JdbcUtils.changeDataSource(Constants.COLLECT_DB);
        novaAreaPointService.findActivetime(starttime, endtime, areaPointList, ModelConstant.LIGHT_ON);
        JdbcUtils.changeDataSource(Constants.DEFAULT_DB);
        double energy = novaAreaPointService.getEnergy(areaPointList);
        double energyByDatePoint = novaGatherSummaryService.findEnergyByDatePoint(areaPointList, ModelConstant.MIN_DATE, ModelConstant.MAX_DATE);
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(energy + energyByDatePoint);
    }

    @RequestMapping(value = "/areaConsume.shtml")
    public ModelAndView areaConsume(HttpServletRequest request,
                                    HttpSession session, ModelMap modelMap) {
        Login login = (Login) session.getAttribute("sessionLogin");
        List<Map<String, Object>> points = novaAreaPointService.findPointOnly(login.getCustomerinfoid());
        String endDate = DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD);
        modelMap.put("points", points);
        modelMap.put("endDate", endDate);
        return new ModelAndView("statistics/areaConsume", modelMap);
    }


    @RequestMapping(value = "/areaState.shtml")
    @ResponseBody
    public List<Gather> areaState(HttpServletRequest request,
                                  HttpSession session, ModelMap modelMap, Integer customerinfoid, Integer pointid, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (customerinfoid == null || customerinfoid == 0) {
            customerinfoid = login.getCustomerinfoid();
        }
        //   CustomerInfo customerInfo = novaCustomerInfoService.getById(customerinfoid);
        List<Gather> gatherList = null;
        if (!IntegerUtils.isEmptyAndNull(pointid)) {
            return gatherList;
        } else {
            AreaPoint point = novaAreaPointService.findById(pointid);
            if (endDate == null || "".equals(endDate) || DateUtils.getDate2SStr(new Date()).equals(endDate)) {
                endDate = DateUtils.getDate2SStr(new Date());
                JdbcUtils.changeDataSource(Constants.COLLECT_DB);
                gatherList = novaGatherService.findOneToday(endDate + " 00:00:00", endDate + " 23:59:59", point, ModelConstant.GATHER_ERRORSTATE);
                JdbcUtils.changeDataSource(Constants.DEFAULT_DB);
            } else {
                Gather gather = new Gather();
                gather.setPointid(point.getId());
                gatherList = novaGatherService.findExampleOnedate(endDate, gather);
            }
            List<Gather> gathers = novaGatherService.disposeforchart(gatherList, endDate);
            return gathers;
        }
    }

    @RequestMapping(value = "/getAreas.shtml")
    @ResponseBody
    public List<Map<String, Object>> getAreas(HttpServletRequest request,
                                              HttpSession session, ModelMap modelMap, Integer customerinfoid) {
        List<Map<String, Object>> points = novaAreaPointService.findPointOnly(customerinfoid);
        return points;
    }
}
